﻿//////////////////////////////////////////////////////////////////////////////
//
//  Copyright © 1998-2024 Glodon Company Limited.  All rights reserved.
//
//  Use of this software is subject to the terms of the Glodon license
//  agreement provided at the time of installation or download, or which
//  otherwise accompanies this software in either electronic or hard copy form.  
//
//////////////////////////////////////////////////////////////////////////////

#pragma once
#include "NdbObjectUnserializeResult.h"
#include "NdbObject.h"
#include "IIOStream.h"
#include "NdbPropertySchemaType.h"

namespace gcmp
{
    class NdbMemoryIODevice;
    class NdbObjectMemoryModelNode;
    class NdbPropertySchema;
    class IDbObject;
    class NdbArchiveBlockHeader;
    class IDbObjectArchive;

    /// \brief 自定义升级辅助类，平台内部使用
    class GCMP_DATABASE_EXPORT NdbCustomUpgraderPropertyUtils
    {
    public:
        NdbCustomUpgraderPropertyUtils();

        ~NdbCustomUpgraderPropertyUtils();

        /// \brief 创建一个NdbObjectArchive对象
        /// \return NdbObjectArchive对象
        OwnerPtr<IDbObjectArchive> CreateNdbObjectArchive();

        /// \brief 拷贝一个NdbObject对象
        /// \param pObjectMemoryModelNode 内存模型节点指针
        /// \param propertyName 属性名称
        /// \param pNewObject 对象指针
        /// \return 拷贝成功返回true，否则返回false
        static bool CopyNdbObject(const NdbObjectMemoryModelNode* pObjectMemoryModelNode, const std::wstring& propertyName, NdbObject* pNewObject);

        /// \brief 得到NdbObject对象，使用OwnerPtr包装
        /// \param pObjectMemoryModelNode 内存模型节点指针
        /// \param propertyName 属性名称
        /// \return NdbObject对象
        static OwnerPtr<NdbObject> GetOwnedNdbObject(const NdbObjectMemoryModelNode* pObjectMemoryModelNode, const std::wstring& propertyName);

        /// \brief 得到IDbObject对象，使用OwnerPtr包装
        /// \param pObjectMemoryModelNode 内存模型节点指针
        /// \param propertyName 属性名称
        /// \return IDbObject对象
        static OwnerPtr<IDbObject> GetOwnedIDbObject(const NdbObjectMemoryModelNode* pObjectMemoryModelNode, const std::wstring& propertyName);

        /// \brief 得到IDbObject对象，使用OwnerPtr包装
        /// \param pObjectMemoryModelNode 内存模型节点指针
        /// \param propSchema 属性名称
        /// \param ioDevice io设备对象
        /// \return IDbObject对象
        static bool CreateAndReplacePropertyNode(const NdbPropertySchema& propSchema, IIODevice& ioDevice, NdbObjectMemoryModelNode* pObjectMemoryModelNode);

        /// \brief 得到一个NdbOjbect对象的长度
        /// \param archive IDbObjectArchive指针对象
        /// \param pPropObject 对象指针
        /// \param pPropClassSchema classSchema指针
        /// \return NdbOjbect对象的长度
        static UInt64 GetNdbObjectLength(OwnerPtr<IDbObjectArchive>& archive, const NdbObject* pPropObject, const NdbClassSchema* pPropClassSchema);

        /// \brief 序列化一个属性NdbObject的Body数据并返回长度
        /// \param archive IDbObjectArchive指针对象
        /// \param pPropObject 属性对象指针
        /// \return 属性NdbOjbect对象的Body数据长度
        static UInt64 SerializeNdbObjectBody(OwnerPtr<IDbObjectArchive>& archive, const NdbObject* pPropObject);

    private:
        OwnerPtr<NdbMemoryIODevice> m_pMemOutputStream;
    };

}

